package com.qc31.baselib.base

import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle

import android.view.LayoutInflater
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import autodispose2.androidx.lifecycle.AndroidLifecycleScopeProvider
import com.qc31.baselib.R
import com.qc31.baselib.appstate.AppStatus
import com.qc31.baselib.appstate.AppStatusManger
import com.qc31.baselib.custom.MLoadingDialog
import com.qc31.baselib.custom.MessageDialog
import com.qc31.baselib.entity.ToastEntity
import com.qc31.baselib.network.NetUtil
import com.qc31.baselib.utils.AppManager
import com.qc31.baselib.utils.Language
import com.qc31.baselib.utils.toast
import com.qc31.baselib.network.NetChangerReceiver
import com.qc31.baselib.network.OnNetChangerListener
import kotlin.system.exitProcess

abstract class BaseActivity<VB : ViewBinding>(private val inflater: (LayoutInflater) -> VB) :
    AppCompatActivity(), OnNetChangerListener {

    protected val scopeProvider: AndroidLifecycleScopeProvider by lazy {
        AndroidLifecycleScopeProvider.from(this)
    }

    private var dialog: MessageDialog? = null

    lateinit var binding: VB


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (AppStatusManger.instance.mAppStatus == AppStatus.STATUS_KILLED) {
            val intent = baseContext.packageManager
                .getLaunchIntentForPackage(baseContext.packageName)
            intent!!.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
            startActivity(intent)
            exitProcess(0)
            return
        }
        binding = inflater(layoutInflater)
        setContentView(binding.root)
        initView()
        setListener()
        AppManager.attach(this)
        NetChangerReceiver.addListener(this)
    }

    /** 初始化界面 */
    abstract fun initView()

    abstract fun setListener()

    override fun onResume() {
        super.onResume()
        onDisconnect(NetUtil.isAvailable(this))
    }

    override fun onDestroy() {
        super.onDestroy()
        AppManager.detach(this)
        dismissDialog()
        dialog = null
        MLoadingDialog.clear()
//        MToast.cancel()
        NetChangerReceiver.removeListener(this)
    }


    /** 断网监听处理 */
    override fun onDisconnect(isDisconnect: Boolean) {
        if (!isDisconnect)
            showNetDialog(R.string.toast_net_available)
        else dismissDialog()
    }


    /** 显示Dialog */
    private fun showNetDialog(@StringRes message: Int) {
        showDialog(getString(message))
    }

    /** 网络弹窗 */
    private fun showDialog(message: String) {
        dialog = dialog ?: MessageDialog(this, positive = R.string.dialog_setting, clickListener = {
            NetUtil.openWirelessSettings(this)
        })
        dialog?.show(message)
    }

    /** 隐藏Dialog */
    private fun dismissDialog() {
        dialog?.apply {
            if (isShowing) dismiss()
        }
    }

    fun isShowLoading(isShow: Boolean) {
        if (isShow) MLoadingDialog.showLoading(this) else MLoadingDialog.dismiss()
    }

    fun toastObserver(entity: ToastEntity) {
        if (entity.toast.isNotEmpty()) {
            toast(entity.toast)
            return
        }
        if (entity.toastId != 0) {
            toast(entity.toastId)
            return
        }
        isShowLoading(entity.isShowLoading)
    }

    override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
        // 兼容androidX在部分手机切换语言失败问题
        if (overrideConfiguration != null) {
            val uiMode: Int = overrideConfiguration.uiMode
            overrideConfiguration.setTo(baseContext.resources.configuration)
            overrideConfiguration.uiMode = uiMode
        }
        super.applyOverrideConfiguration(overrideConfiguration)
    }

    /** 设置语言 */
    override fun attachBaseContext(context: Context) {
        super.attachBaseContext(Language.instance.attachBaseContext(context))
    }

}